package cn.bdqn.controller;

import cn.bdqn.pojo.Right;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import cn.bdqn.service.RightService;
import cn.bdqn.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Resource
    UserService userService;
    @Resource
    RightService rightService;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    /**
     * 跳转登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 跳转首页
     *
     * @return
     */
    @RequestMapping("/main")
    public String main() {
        return "main";
    }

    /**
     * 登录
     *
     * @param usrName
     * @param usrPassword
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, boolean rememberme,
                          Model model, HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(usrName, usrPassword, rememberme);
        try {
            subject.login(token);
            //登录成功
            User user = (User) subject.getPrincipal();
            //授权代码
            //获取用户的角色
            Role role = user.getRole();
            //查询角色所有权限
            List<Right> rights = rightService.findRightsByRoleId(role.getRoleId());
            role.getRights().addAll(rights);
            session.setAttribute("loginUser", user);
            stringRedisTemplate.delete("shiro_login_count" + token.getUsername());
            //重定向到main方法
            return "redirect:/main";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getMessage());
            return "forward:/login";
        } catch (LockedAccountException e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getMessage());
            return "forward:/login";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getMessage());
            return "forward:/login";
        }
    }


        /**
         * 退出
         * @param session
         * @return
         */
        @RequestMapping("/logout")
        public String logout(HttpSession session){
            session.removeAttribute("loginUser");
            SecurityUtils.getSubject().logout();
            return "forward:/login";
        }

    /**
     * 跳转403页面
     * @return
     */
    @RequestMapping("/403")
    public String authorization(){
        return "403";
    }
    }


